var material_add = new Object();
material_add.businessIds = [];

material_add.init = function(){
    material_add.getData();
    material_add.initBusinessTree();
    material_add.handle();
    material_add.deleteDept();
}

material_add.getData = function () {
    $("#business_select2").select2({
        tags:true,
        data:business_json,
        multiple:true,
        placeholder:"请选择行业",
        language:"zh-CN"
    })
}

//行业
material_add.business_zTree;
material_add.initBusinessTree = function () {
    // 加载树
    material_add.business_zTree = $.fn.zTree.init($("#group_tree"), {
        check: {
            enable: true,
            chkboxType :  { "Y" : "s", "N" : "s" }
            //nocheckInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            dblClickExpand: false
        },
        callback: {
            onCheck: function (event, treeId, treeNode) {
                var nodes = material_add.business_zTree.getCheckedNodes(true);
                material_add.businessIds = [];
                var businessName = [];
                var n;
                for(var i = 0; i < nodes.length; i++){
                    var n = nodes[i];
                    if(n.pId == null){
                        material_add.businessIds = [];
                        material_add.businessIds.push(n.id);
                        businessName = [];
                        businessName.push(n.name);
                        $("#business_select2").val(material_add.businessIds).trigger("change");
                        if(nodes.length > 1){
                            material_add.cancelNode(material_add.business_zTree, n);
                        }
                        $("#businessId").val(material_add.businessIds.join(","));
                        $("#businessName").val(businessName.join(","));
                        return;
                    }
                    material_add.businessIds.push(n.id);
                    businessName.push(n.name);
                }
                $("#businessId").val(material_add.businessIds.join(","));
                $("#businessName").val(businessName.join(","));
                //前台显示并查询
                $("#business_select2").val(material_add.businessIds).trigger("change");
                // material_add.deleteDept();
            }
        }
    }, business_tree);


    //点击小X触发，取消树中节点的勾选状态
    material_add.deleteDept = function () {
        $(document).delegate('.select2-selection__choice__remove','click',function(){
            var id = $(this).parent().attr('id');
            var treeObj = $.fn.zTree.getZTreeObj("group_tree");
            var node = treeObj.getNodeByParam("id", id, null);
            node.checked = false ;
            treeObj.updateNode(node);
            var checkedNodes = treeObj.getCheckedNodes(true);
            var businessName = [];
            material_add.businessIds = [];
            if(checkedNodes.length > 0){
                var businessName = [];
                material_add.businessIds = [];
                if(id == '1'){  //全部行业
                    treeObj.checkAllNodes(false);
                }else{
                    for(var i=0; i< checkedNodes.length; i++) {
                        var n = checkedNodes[i];
                        material_add.businessIds.push(n.id);
                        businessName.push(n.name);
                    }
                }
            }
            $("#businessId").val(material_add.businessIds.join(","));
            $("#businessName").val(businessName.join(","));
        })
    }
};

material_add.selectBusiness=function(){
    //得到最新的受训单位数据
    var businesses = $("#business_select2").select2("data");
    material_add.getNewDate(businesses);
    $('.por').addClass('show');

};

    //得到最新的受训单位数据

    material_add.getNewDate=function(data){
        var businessData = [];
        for(var i = 0;i<data.length;i++){
            businessData.push(data[i].id);
        }
        material_add.businessIds = businessData;
    }

material_add.cancelNode = function (treeObj, node) {
    treeObj.checkAllNodes(false);
    treeObj.checkNode(node, true, true);
};

material_add.getAllchildrenNodesId = function (treeNode) {
    var array = [];
    array.push(treeNode.id);
    if(treeNode.isParent){
        var childrenNodes = treeNode.children;
        if(childrenNodes){
            for(var i = 0; i<childrenNodes.length; i++){
                // array.push(childrenNodes[i].id);
                array = array.concat(material_add.getAllchildrenNodesId(childrenNodes[i]))
            }
        }
    }
    return array;
};

material_add.getAllchildrenNodesName = function (treeNode) {
    var array = [];
    array.push(treeNode.name);
    if(treeNode.isParent){
        var childrenNodes = treeNode.children;
        if(childrenNodes){
            for(var i = 0; i<childrenNodes.length; i++){
                array = array.concat(material_add.getAllchildrenNodesName(childrenNodes[i]))
            }
        }
    }
    return array;
};

material_add.handle = function () {
    //返回
    $("#btn_back").click(function () {
        parent.layer.closeAll();
    });
    //保存
    $("#save").click(function () {
        var businessId = $("#businessId").val();
        /*if(!businessId){
            layer.msg('请选择行业', {icon: 2});
            return;
        }*/
        var fileId = $("#varFid").val();
        var sourceFile = {};
        if(!fileId){
            layer.msg('请上传图片', {icon: 2});
            return;
        }else{
            sourceFile.fileId = fileId;
            var fileName = $("#varFileName").val();
            sourceFile.fileName = fileName;
            var width = $("#w").val();
            sourceFile.width = width;
            var height = $("#h").val();
            sourceFile.height = height;
        }
        if(sourceFile.fileId){
            sourceFile = JSON.stringify(sourceFile);
        }else{
            sourceFile = "";
        }
        var params = {
            "typeId": $("#typeId").val(),
            "businessId": businessId,
            "businessName": $("#businessName").val(),
            "sourceFile": sourceFile,
            "sourceDesc": $("#sourceDesc").val()
        }
        $.ajax({
            url: appPath + "/admin/hiddenMaterial/save",
            type: 'post',
            data: params,
            success: function (data) {
                if("10000" == data.code){
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.msg('保存成功');
                    parent.layer.close(index);
                    parent.hiddenMaterial.initListTable();
                }else{
                    layer.alert('操作失敗', {icon: 2, skin: 'layer-ext-moon'});
                }
            }
        });
    })

};




